package com.imooc.service.impl;

import com.imooc.common.dto.OrderDTO;
import com.imooc.common.enums.ResultEnum;
import com.imooc.common.exception.SelllException;
import com.imooc.service.BuyerSercice;
import com.imooc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BuyerSerciceImpl implements BuyerSercice {

    @Autowired
    OrderService orderService;

    @Override
    public OrderDTO findOrderOne(String orderId, String openid) {
        OrderDTO orderDTO = orderService.findOne(orderId);
        if (!openid.equals(orderDTO.getBuyerOpenid())) {
            throw new SelllException(ResultEnum.ORDER_OWANER_ERROR);
        }
        return orderDTO;
    }

    @Override
    public OrderDTO cancel(String orderId, String openid) {
        OrderDTO orderDTO = orderService.findOne(orderId);
        if (!openid.equals(orderDTO.getBuyerOpenid())) {
            throw new SelllException(ResultEnum.ORDER_OWANER_ERROR);
        }

        return orderService.cancel(orderDTO);
    }
}
